package com.lft.examsys.model;

import com.lft.examsys.exception.ViewChangeException;
import com.lft.examsys.handler.ValidationFailureHandler;
import com.lft.examsys.handler.ValidationHandler;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

public class Model extends HashMap<String, Object> implements IModel {
    private String view = null;
    private boolean valid = true;

    @Override
    public ModelAndView redirectTo(String path) throws ViewChangeException {
        view = "redirect:" + path;
        throw new ViewChangeException();
    }

    @Override
    public ModelAndView forwardTo(String viewName) throws ViewChangeException {
        view = viewName;
        throw new ViewChangeException();
    }

    @Override
    public String currentView() {
        return view;
    }

    @Override
    public ValidationModelAndView validation(String viewName, ValidationHandler valid, ValidationFailureHandler invalid) {
        return new ValidationModelAndView(viewName, valid, invalid);
    }

    @Override
    public ValidationModelAndView validation(String viewName, ValidationHandler valid) {
        return new ValidationModelAndView(viewName, valid, null);
    }

    @Override
    public boolean valid() {
        return valid;
    }

    @Override
    public IModel valid(boolean valid) {
        this.valid = valid;
        return this;
    }
}
